﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace TiledPlatformer.Entity
{
    public class MovableObject : GameObject
    {
        protected Vector2 Acceleration;
        public Vector2 Velocity;
        protected Rectangle bottomBox, leftBox, rightBox, topBox;

        private const int SUBBOX_WIDTH = 2;

        public MovableObject(Vector2 position, Texture2D image)
            :base(position, image)
        {
            resizeSubshapes();
            //collisionBox.Inflate(2, 2);
        }

        public virtual void Update()
        { 
            //leave it up to children to decide how to update
            UpdatecollisionBoxes();
        }

        public override void Draw(SpriteBatch batch)
        {
            Texture2D defaultTexture = new Texture2D(Platformer.graphics.GraphicsDevice, 1, 1, true, SurfaceFormat.Color);
            defaultTexture.SetData(new[] { Color.White });

            batch.Draw(defaultTexture, bottomBox, Color.Green);
            batch.Draw(defaultTexture, leftBox, Color.Yellow);
            batch.Draw(defaultTexture, rightBox, Color.Blue);
            batch.Draw(defaultTexture, topBox, Color.Red);
            base.Draw(batch);
        }

        private void UpdatecollisionBoxes()
        {
            int oldX = collisionBox.X;
            int oldY = collisionBox.Y;
            int newX = (int)position.X;
            int newY = (int)position.Y;

            collisionBox.X = newX;
            collisionBox.Y = newY;

            bottomBox.X += (newX - oldX);
            bottomBox.Y += (newY - oldY);

            topBox.X += (newX - oldX);
            topBox.Y += (newY - oldY);

            leftBox.X += (newX - oldX);
            leftBox.Y += (newY - oldY);

            rightBox.X += (newX - oldX);
            rightBox.Y += (newY - oldY); 
        }

        private void resizeSubshapes()
        {
            bottomBox = new Rectangle((int)position.X, (int)position.Y + collisionBox.Height, collisionBox.Width, SUBBOX_WIDTH);
            topBox = new Rectangle((int)position.X, (int)position.Y - SUBBOX_WIDTH, collisionBox.Width, SUBBOX_WIDTH);
            leftBox = new Rectangle((int)position.X - SUBBOX_WIDTH, (int)position.Y, SUBBOX_WIDTH, collisionBox.Height);
            rightBox = new Rectangle((int)position.X + collisionBox.Width, (int)position.Y, SUBBOX_WIDTH, collisionBox.Height);
        }
    }
}
